package com.example.Arithmetic.Arithmetic;

import java.util.Arrays;

/**
 * 日期：2023/12/26
 * 时间：22:34
 * 描述：插入排序，无递归实现
 */
public class InsertionSorting {
    public void sort(int[] ints) {
        for (int i = 1; i < ints.length; i++) {
            int t = ints[i];
            int w = i - 1;
            while (w>=0&&t<ints[w]){
                ints[w + 1] = ints[w]; // 空出插入位置
                w--;
            }
            // 找到插入位置
            if (w != i - 1) {
                ints[w + 1] = t;
            }
        }
    }

    public static void main(String[] args) {
        int[] a = {2, 3, 1, 7, 6, 4, 5};
        System.out.println(Arrays.toString(a));
        InsertionSorting insertionSorting = new InsertionSorting();
        insertionSorting.sort(a);
        System.out.println(Arrays.toString(a));
    }
}
